App({
  onLaunch: function(options) {
    var that = this
    that.login()
    // wx.checkSession({
    //   success () {

    //   },
    //   fail () {
    //     that.login() //重新登录
    //   }
    // })
     // 默认进入授权
     if(!wx.getStorageSync('id')){
       wx.redirectTo({
         url: '/pages/authorization/authorization'
       })
     }

    if (wx.getStorageInfoSync('vip_id') == '' || wx.getStorageInfoSync('vip_id') <= 0){
      wx.redirectTo({
        url: '/pages/authorization/authorization'
      })
    }

     wx.getSetting({
      success(res) {
        if(!res.authSetting['scope.userInfo']) {
         wx.redirectTo({
           url: '/pages/authorization/authorization'
         })
        }
      }
    })
  },
  login: function(param) {
    var that = this;
    wx.login({
      success: function(res) {
        if (res.code) {
          wx.request({
            url: that.urlData.getURL + "/index.php/api/SApi/index/key/" + that.urlData.key + "/m/actionAuth/wid/" + that.urlData.wid,
            data: {
              code: res.code
            },
            success: (res) => {
              console.log(res.data);
              if (res.data.status == 1) {
                wx.setStorageSync('sign', res.data.sign)
                wx.setStorageSync('vip_id', res.data.vip_id)
                wx.setStorageSync('user_id', res.data.ydnshop_uid)
                wx.setStorageSync('customerid', res.data.customerid);
              }
              if (param) {
                param(res);
              }
            },
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg);
        }
      }
    });
  },
  getUserInfo: function(sign, nickName, avatarUrl, gender, encryptedData, iv, cb = null) {
    if (nickName == "undefined" && sex == "undefined" && headimgurl == 'undefined') {
      console.log('用户信息获取失败')
    } else {
      var that = this;
      wx.request({
        url: that.urlData.getURL + '/index.php/Api/SApi/index/key/' + that.urlData.key + '/m/updateUserInfoXCX/wid/' + that.urlData.wid + "/sign/" + wx.getStorageSync('sign'),
        method: 'POST',
        header: {
          "content-type": 'application/x-www-form-urlencoded'
        },
        data: {
          nickName: nickName,
          head_pic: avatarUrl,
          sex: gender,
          encryptedData: encryptedData,
          iv: iv
        },
        success: function(res) {
          typeof cb == "function" && cb(res);
        }
      })
    }
  },
  shareMore: function(e) {
    wx.showToast({
      title: '感谢分享！',
      icon: 'none',
      duration: 2000
    })
    if (e.shareTickets[0]) {
      let that = this;
      wx.request({
        url: that.urlData.getURL + '/index.php/mobile/Shopindex/share',
        method: 'get',
        data: {
          user_id: wx.getStorageSync('ydn_id')
        },
        success: (res) => {
          wx.showToast({
            title: '感谢分享！',
            icon: 'none',
            duration: 2000
          })
        }
      })
    }
  },
  urlData: {
    key: '2sdfdd332342341dddff46e189670',
    wid: '45',
    sign: '',
    getURL: 'https://mall.paparela.cn', 
    // getURL: 'https://malltest.paparela.cn',  //测试
    urlBind: 'https://api.hepulanerp.com'
  },
  globalData: {
    user_id: wx.getStorageSync('user_id'),
    userInfo: wx.getStorageSync('userInfo'),
    userInfo: null,
    classifyId: null,
    flag: false
  },
})
